Passed
Branch develop (e93b74)
by Endre
03:49
created

Action.switchPage   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
import {IObserver} from '../Observer/Observer';
2
import RouterRegistry from '../Router/Registry';
3
import Router, {IPageData} from '../Router/Router';
4
import {IAdapter, IModulePageData} from './Application';
5
import ModuleLoader from './ModuleLoader';
6
7
export default class Action {
8
  menuOpenState: IObserver<boolean>;
9
  router: Router;
10
  routerRegistry: RouterRegistry;
11
  moduleLoader: ModuleLoader;
12
13
  constructor(
14
    menuOpenState: IObserver<boolean>,
15
    router: Router,
16
    routerRegistry: RouterRegistry,
17
    moduleLoader: ModuleLoader
18
  ) {
19 6
    this.menuOpenState = menuOpenState;
20 6
    this.router = router;
21 6
    this.routerRegistry = routerRegistry;
22 6
    this.moduleLoader = moduleLoader;
23
  }
24
25
  get adapter(): IAdapter {
26 6
    return {
27
      onPageChanged: this.loadModule.bind(this),
28
      onGithubClick: this.openGithubWindow.bind(this),
29
      onMenuClick: this.switchMenuState.bind(this),
30
      onClose: this.closeMenu.bind(this),
31
      onMenu: this.switchPage.bind(this)
32
    };
33
  }
34
35
  protected openGithubWindow(): void {
36 1
    window.open('https://github.com/enbock/Time-Tracker/', '_blank');
37
  }
38
39
  protected switchMenuState(): void {
40 1
    this.menuOpenState.value = !this.menuOpenState.value;
41
  }
42
43
  protected closeMenu(): void {
44 3
    this.menuOpenState.value = false;
45
  }
46
47
  protected switchPage(name: string): void {
48 1
    const page: IPageData = this.routerRegistry.getPages()[name];
49 1
    this.router.changePage(page);
50
  }
51
52
  protected async loadModule(oldValue: IPageData, newValue: IPageData) {
53 1
    await this.moduleLoader.loadModule((newValue as IModulePageData).module);
54 1
    this.closeMenu();
55
  }
56
}
57